<template>
  <div class="addnode">
    <div class="nodeContent">
      <el-popover placement="right-start" v-model="visible">
        <div class="nodebody">
          <a class="nodeitem approver" @click="addType('approval')">
            <div class="item">
              <span class="iconfont"></span>
            </div>
            <span>审批人</span>
          </a>
          <a class="nodeitem condition" @click="addType('route')">
            <div class="item">
              <span class="iconfont"></span>
            </div>
            <span>条件分支</span>
          </a>
        </div>
        <button class="btn" type="button" slot="reference">
          <span class="iconfont"></span>
        </button>
      </el-popover>
    </div>
  </div>
</template>
<script>
import { getKeyRandom, addCondition } from './workflow'
export default {
  name: 'approveFlowPathAddNode',
  props: ['childNode', 'nodeId'],
  data () {
    return {
      visible: false
    }
  },
  methods: {
    // type：start：发起人 approval：审批人 condition：条件 route：路由
    addType (type) {
      const curNodeId = type + '_' + getKeyRandom()
      const _cacheNode = {
        nodeId: curNodeId,
        prevId: this.nodeId,
        type: type,
        /* type 审批类型
        operaterType 审批人类型
        operaters 审批人
        mode 审批方式
        emptyOperater 无审批人时处理方式
        notifyOf 抄送 */
        propertys: {
          type: 'manual',
          operaterType: 'target',
          operaters: '',
          mode: 'step',
          emptyOperater: 'failed',
          notifyOf: []
        }
      }
      if (type === 'approval') {
        _cacheNode.childNode = this.childNode
        _cacheNode.nameEdit = false
        _cacheNode.name = '审批人'
        _cacheNode.placeholder = '请选择审批人'
      }
      if (type === 'route') {
        _cacheNode.conditionNodes = []
        for (let i = 1; i >= 0; i--) {
          _cacheNode.conditionNodes.push(
            addCondition(i, curNodeId, i ? this.childNode : {})
          )
        }
      }
      this.$emit('update:childNode', _cacheNode)
    }
  }
}
</script>
<style scoped lang="scss">
@font-face {
  font-family: "IconFont";
  src: url("../assets/font_135284_ph2thxxbzgf.eot");
  src: url("../assets/font_135284_ph2thxxbzgf.eot?#iefix")
      format("embedded-opentype"),
    url("../assets/font_135284_ph2thxxbzgf.woff") format("woff"),
    url("../assets/font_135284_ph2thxxbzgf.ttf") format("truetype"),
    url("../assets/font_135284_ph2thxxbzgf.svg#IconFont") format("svg");
}

.iconfont {
  font-family: "IconFont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -webkit-text-stroke-width: 0.2px;
  -moz-osx-font-smoothing: grayscale;
}

.addnode {
  width: 240px;
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  position: relative;

  &:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: -1;
    margin: auto;
    width: 2px;
    height: 100%;
    background-color: #cacaca;
  }

  .nodeContent {
    user-select: none;
    width: 240px;
    padding: 20px 0 32px;
    display: flex;
    -webkit-box-pack: center;
    justify-content: center;
    flex-shrink: 0;
    -webkit-box-flex: 1;
    flex-grow: 1;
    .btn {
      outline: none;
      box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
      width: 30px;
      height: 30px;
      background: #3296fa;
      border-radius: 50%;
      position: relative;
      border: none;
      line-height: 30px;
      -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);

      .iconfont {
        color: #fff;
        font-size: 16px;
      }

      &:hover {
        transform: scale(1.3);
        box-shadow: 0 13px 27px 0 rgba(0, 0, 0, 0.1);
      }

      &:active {
        transform: none;
        background: #1e83e9;
        box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
      }
    }
  }
}

.nodebody {
  display: flex;
  .nodeitem {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
    color: #191f25 !important;
    margin-right: 8px;
    width: 160px;
    background: rgba(17, 31, 44, 0.02);
    padding: 8px;
    border: 1px solid #ffffff;
    border-radius: 4px;
    .item {
      user-select: none;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 40px;
      height: 40px;
      background: #ffffff;
      border: 1px solid #eeeeee;
      border-radius: 16px;
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      margin-right: 12px;

      .iconfont {
        font-size: 20px;
        line-height: 40px;
      }
    }

    &.approver {
      .item {
        color: #ff943e;
      }
    }

    &.notifier {
      .item {
        color: #3296fa;
      }
    }

    &.condition {
      .item {
        color: #15bc83;
      }
    }
  }

  .nodeitem:hover {
    background: #ffffff;
    border: 1px solid #ecedef;
    box-shadow: 0 2px 8px 0 rgba(17, 31, 44, 0.08);
  }
}
</style>
